/*
 * $Id: SecurityServiceTestCase.java 29 2007-04-16 23:57:10Z aaron.day $
 */
package org.crazydays.security;


import java.util.*;

import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;

import org.hibernate.*;

import junit.framework.TestCase;

import org.crazydays.hibernate.HibernateUtil;
import org.crazydays.core.data.PolisApplication;
import org.crazydays.core.data.PolisRight;
import org.crazydays.core.data.PolisUser;

/**
 * SecurityServiceTestCase
 */
public class SecurityServiceTestCase
	extends TestCase
{
	/** security service */
	private ISecurityService securityService;

	/**
	 * Setup.
	 */
	public void setUp()
	{
		Registry registry = RegistryBuilder.constructDefaultRegistry();

		securityService = (ISecurityService) registry
				.getService(ISecurityService.class);
		// TODO: set security service
	}

	/**
	 * Test createCriteria(Session,Class,PolisRight,PolisUser).
	 */
	public void testCreateCriteria()
	{
		Session session = HibernateUtil.getSession();
		try {
			PolisUser adminUser = (PolisUser) HibernateUtil
					.loadPolisDataByName(session,
							PolisUser.class,
							"Admin");
			assertNotNull("adminUser == null", adminUser);

			PolisUser guestUser = (PolisUser) HibernateUtil
					.loadPolisDataByName(session,
							PolisUser.class,
							"Guest");
			assertNotNull("guestUser == null", guestUser);

			PolisRight adminRight = (PolisRight) HibernateUtil
					.loadPolisDataByName(session,
							PolisRight.class,
							"Admin");
			assertNotNull("adminRight == null", adminRight);

			Criteria criteria = null;
			List applications = null;

			criteria = securityService.createCriteria(session,
					PolisApplication.class, adminRight,
					adminUser);
			applications = criteria.list();
			assertNotNull("applications == null", applications);
			
			Iterator i = applications.iterator();
			while (i.hasNext()) {
				System.out.println(i.next());
			}
			
			assertEquals("applications.size != 1", 1, applications
					.size());

			criteria = securityService.createCriteria(session,
					PolisApplication.class, adminRight,
					guestUser);
			applications = criteria.list();

			assertEquals("applications.size != 0", 0, applications
					.size());
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		}
	}
}
